package psn.hui.bus.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import psn.hui.bus.domain.Customer;
import psn.hui.bus.service.CustomerService;
import psn.hui.bus.vo.CustomerVo;
import psn.hui.sys.constant.SysConstant;
import psn.hui.sys.utils.DataGridView;
import psn.hui.sys.utils.JsonResult;

/**
 * 	客户管理控制器
 * @author hui
 *
 */
@RestController
@RequestMapping("customer")
public class CustomerController {
	
	@Autowired
	private CustomerService customerService;
	
	/**
	 * 	插入客户数据
	 * 
	 * @param customer
	 * @return
	 */
	@RequestMapping("addCustomer")
	public JsonResult addCustomer(Customer customer) {
		try {
			if(customerService.addCustomer(customer) > 0) {
				return SysConstant.ADD_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.ADD_ERROR;
	}

	/**
	 * 	根据主键(identity)删除客户
	 * 
	 * @param identity
	 * @return
	 */
	@RequestMapping("deleteCustomer")
	public JsonResult deleteCustomer(String identity) {
		try {
			if(customerService.deleteByIdentity(identity) > 0) {
				return SysConstant.DELETE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.DELETE_ERROR;
	}

	/**
	 * 根据主键(identity)更新客户数据，动态sql(可选字段)
	 * 
	 * @param customer
	 * @return
	 */
	@RequestMapping("updateCustomer")
	public JsonResult updateCustomer(Customer customer) {
		try {
			if(customerService.updateByIdentity(customer) > 0) {
				return SysConstant.UPDATE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.UPDATE_ERROR;
	}

	/**
	 * 根据主键(identity)查询客户
	 * 
	 * @param identity
	 * @return
	 */
	@RequestMapping("loadCustomerByIdentity")
	public Customer loadCustomerByIdentity(String identity) {
		try {
			return customerService.selectByIdentity(identity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new Customer();
	}

	/**
	 * 根据可选字段查询客户
	 * 
	 * @param customerVo
	 * @return
	 */
	@RequestMapping("customerList")
	public DataGridView customerList(CustomerVo customerVo) {
		try {
			return customerService.queryAllCustomer(customerVo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new DataGridView();
	}
	
	/**
	 * 批量删除客户数据
	 * @param customerVo
	 * @return
	 */
	@RequestMapping("batchDeleteCustomer")
	public JsonResult batchDeleteCustomer(CustomerVo customerVo) {
		try {
			System.out.println("identitys:[");
			for (String identity : customerVo.getIdentitys()) {
				System.out.println(identity+",");
			}
			System.out.println("]");
			if(customerService.batchDeleteCustomerByIds(customerVo.getIdentitys()) > 0) {
				return SysConstant.DELETE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.DELETE_ERROR;
	}
	/**
	 * 	检查身份证号是否可用（不重复）
	 * @param identity
	 * @return
	 */
	@RequestMapping("checkIdentity")
	public JsonResult checkIdentity(String identity) {
		try {
			if(customerService.checkIdentity(identity)) {
				return SysConstant.IDENTITY_AVAILABLE;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.IDENTITY_ERROR;
	}
}
